package com.yckj.day10;

/**
 * TODO StringBuilder是可变字符串，线程不安全，用于单线程，速度比StringBuffer快
 *
 * @author shengyuee
 * @version 1.0
 * 2025/7/29 11:58
 */
public class StringBuilderDemo {
    public static void main(String[] args) {
        // 创建
        StringBuilder sb = new StringBuilder();
        StringBuilder sb1 = new StringBuilder("hello");
        // 常用方法
        // 1. 获取字符串长度
        int length = sb1.length();
        System.out.println("length = " + length);
        // 2. 追加字符串
        sb1.append("world");
        System.out.println("sb1 = " + sb1);
        // 3. 插入字符串
        sb1.insert(5, "java");
        System.out.println("sb1 = " + sb1);
        // 4. 删除字符串，不包括后面的索引本身
        sb1.delete(5, 9);
        System.out.println("sb1 = " + sb1);
        // 5. 替换字符串
        sb1.replace(0, 5, "hello world");
        System.out.println("sb1 = " + sb1);
        // 6. 获取指定索引的字符
        char c = sb1.charAt(0);
        System.out.println("c = " + c);
        // 7. 截取指定索引位置的字符串
        String s = sb1.substring(0, 5);
        System.out.println("s = " + s);
        // 8. 获取字符串中指定字符的索引值
        int index = sb1.indexOf("l");
        System.out.println("index = " + index);
        // 9.字符串反转
        sb1.reverse();
        System.out.println(sb1);

    }
}
